var Form = new Class({
	
	initialize: function()
	{
		this.form = [];
		
	},
	
	CreateForm: function(FormID)
	{
		this.form[FormID] = {
			FormID: FormID,
			onSubmit: null,
			Elements: new Array()
		};
		
		return FormID;
	},
	
	CreateInput: function(form, id, name, value, validation)
	{
		if (!this.form[form])
			return;
		
		this.form[form]['Elements'] = this.form[form]['Elements'].concat({
			type: 'input',
			id: id,
			name: name,
			value: value,
			validation: validation
		});
	},
	
	CreateInputP: function(form, id, name, value, validation)
	{
		if (!this.form[form])
			return;
		
		this.form[form]['Elements'] = this.form[form]['Elements'].concat({
			type: 'inputP',
			id: id,
			name: name,
			value: value,
			validation: validation
		});
	},
	
	CreateSubmit: function(form, display)
	{
		if (!this.form[form])
			return;
		
		this.form[form]['Elements'] = this.form[form]['Elements'].concat({
			type: 'submit',
			value: display
		});
	},
	
	OnSubmit: function(form, func, cls)
	{
		if (!this.form[form])
			return;

		this.form[form]['onSubmit'] = {func: func, cls: cls};
		
	},
	
	triggerSubmit: function(form)
	{
		if (!this.form[form])
			return;
		
		var ele = null;
		var value = "";
		
		var data = new Array();
		
		// Haal alle data op die is ingevuld
		for (var i = 0; i < this.form[form].Elements.length; i++)
		{
			ele = this.form[form].Elements[i];
		
			if (ele == null)
				return;
			
			value = this.getValue(form, ele['id']);
			
			data[ele['id']] = value;
			
		} // end for
		
		if (this.form[form]['onSubmit']['func'] != null)
		{
			this.form[form]['onSubmit']['func'](data, this.form[form]['onSubmit']['cls']);
		}
	},
	
	getValue: function(form, id)
	{
		if (id == null)
			return;
		
		var value = document.forms[form].elements[id].value;
		
		return value;
		
	}, // end function getValue
	
	GenerateHTML: function(form)
	{
		if (this.form[form])
		{
			var code  = 
				'<form name="' + this.form[form]['FormID'] + '">' + 
				'<table width="100%" cellspacing=0 cellpadding=3>' + "\n";
			
			var ele;
			var ele_code;
			
			for (var i = 0; i < this.form[form].Elements.length; i++)
			{
				ele = this.form[form].Elements[i];
				
				if (ele == null)
					return;
				
				ele_code = this.GenerateInput(form, ele);
				
				if (ele['name'] == null)
					code += "<tr><td width=30%>&nbsp;</td><td>" + ele_code + "</td></tr>";
				else
					code += "<tr><td width=30%>" + ele['name'] + "</td><td>" + ele_code + "</td></tr>";
				
			}
			
			code += "</form></table>";
			
			return code;
			
		} // end if
		else
			return '<b>Error!</b>: Form ' + form + ' is not created.';
		
	},
	
	GenerateInput: function(form, ele)
	{
		if (ele['value'] == null)
			ele['value'] = '';
		
		switch(ele['type'])
		{
			case 'submit':
				if (ele['value'] == '')
					ele['value'] = 'Submit';
				
				return '<input type=button value="' + ele['value'] + '" onclick="form_submit(\'' + this.form[form]['FormID'] + '\');">';
				
			break;
			
			case 'input':
				return '<input type=text name="' + ele['id'] + '" value="' + ele['value'] + '">';
				
			break;
			
			case 'inputP':
				return '<input type=password name="' + ele['id'] + '" value="' + ele['value'] + '">';
				
			break;
			
		} // end switch
		
		return false;
		
	}
	
	
});

var FormInst = new Form();

form_submit = function(name)
{
	FormInst.triggerSubmit(name);
	
	return false;
}

cimport_loaded('core.Form');